package ar.com.larctest.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.List;

import ar.com.larctest.R;
import ar.com.larctest.entities.HorsesRacing;
import ar.com.larctest.utils.Constants;

public class ListViewRaceHorseAdapter extends ArrayAdapter<HorsesRacing>
{
	public List<HorsesRacing> items;
	Activity activity;

	public ListViewRaceHorseAdapter(Activity activity, int textViewResourceId, List<HorsesRacing> items) {
		super(activity, textViewResourceId, items);
		this.items = items;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public HorsesRacing getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return (items.get(position)).getIdHorseRacing();
	}

	private class ViewHolder {
	    TextView dateRace;
	    TextView distance;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		LayoutInflater inflater =  activity.getLayoutInflater();

        SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATE);
		
		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.listitem_race_horse, null);
			holder = new ViewHolder();
			holder.dateRace = (TextView) (convertView != null ? convertView.findViewById(R.id.dateRace) : null);
			holder.distance = (TextView) (convertView != null ? convertView.findViewById(R.id.distance) : null);

            if (convertView != null) {
                convertView.setTag(holder);
            }
        }
		else
			holder = (ViewHolder) convertView.getTag();

        HorsesRacing raceItem = items.get(position);
		holder.dateRace.setText(sdf.format(raceItem.getDateRacing()));
		holder.distance.setText(raceItem.getDistance().toString());

		return convertView;	
	}
}
